package com.haierCamera.customapplication.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.RegisterRepo;
import com.haierCamera.customapplication.databinding.HzklActivityForgetBinding;
import com.haierCamera.customapplication.utils.EditTextChangeBoolean;
import com.haierCamera.customapplication.utils.android.SystemUIHelper;

import javax.inject.Inject;

public class HZKLForgetActivity extends BaseActivity {
    private HzklActivityForgetBinding binding;
    @Inject
    RegisterRepo repo;
    ////密码只包含字母，数字，字符中至少两种
    private static final String regex ="^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{8,32}$";
    boolean phoneEnable, pwdEnable, aBoolean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemUIHelper.setFullWindowNew(getWindow());
        SystemUIHelper.setStatusBarTextColor(getWindow(), true);
        binding= DataBindingUtil.setContentView(this, R.layout.hzkl_activity_forget);
        initData();
    }

    private void initData() {
        binding.loginPwdLayout.setPasswordVisibilityToggleDrawable(R.drawable.hzkl_pwd_selector);
        binding.forgetBack.setOnClickListener(view -> finish());
        binding.forgetBtn.setOnClickListener(view -> forgetUser());
        new EditTextChangeBoolean().buttonEnable(binding.forgetPhone, enable -> {
            phoneEnable = enable;
            binding.forgetBtn.setEnabled(phoneEnable && pwdEnable);
        });
        new EditTextChangeBoolean().buttonEnable(binding.forgetPwd, enable -> {
            pwdEnable = enable;
            binding.forgetBtn.setEnabled(phoneEnable && pwdEnable);
        });
    }

    private void forgetUser() {
        String phone = binding.forgetPhone.getText().toString();
        String pwd = binding.forgetPwd.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!pwd.matches(regex)) {
            Toast.makeText(this, "密码格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        repo.getVcode(phone).observe(this, objectResource -> {
            switch (objectResource.status){
                case LOADING:
                    break;
                case ERROR:
                    Toast.makeText(HZKLForgetActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS:
                    startActivityForResult(new Intent(HZKLForgetActivity.this, HZKLVcodeActivity.class)
                            .putExtra("phone", phone).putExtra("pwd", pwd).putExtra("type",2), 100);
                    break;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            finish();
        }
    }
}
